package thorsten.wordlist.parser;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashSet;
import java.util.Set;

public class SingleFileOutputHandler {
//	private FileWriter outfile;
	private Writer outfile;
	private String buffer = "";
	private HashSet<String> stringSet;
//	private int flushSize = 1000000000;
	private int flushSize = 500000 ;
	private int writeCount = 0;
	private int wordCount = 0;

	public SingleFileOutputHandler() throws IOException {
		super();
		//TODO UTF-8 einstellen, z.B. http://stackoverflow.com/questions/1001540/how-to-write-a-utf-8-file-with-java
//		this.outfile = new FileWriter("output/output.txt");
		
		this.outfile = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output/output.txt"), "UTF-8"));
		
//		this.outfile = new FileWriter("D:/home/projekte/wordlist/data/output/output.txt");
		this.stringSet = new HashSet<String>(); 
	}

	public void add(String data) throws IOException {
		this.buffer += data;
	}

	public void flush() throws IOException {
		this.buffer = this.buffer.trim();
		if(buffer.length() == 0)
			return;
		
		// TODO: evtl. beide splits verwenden?
		String[] words=this.buffer.split("\\s");
		String[] words2=this.buffer.split("\\W");
//		System.out.println(words.length);
		
		for(String curWord : words){
			this.stringSet.add(curWord);
		}				
		for(String curWord : words2){
			this.stringSet.add(curWord);
		}				
		
		this.buffer="";
		
		this.writeSet();
	}

	public void finish() throws IOException {
		this.flush();
		this.writeSet(true);
		this.outfile.close();
	}

	public void writeSet() throws IOException{
		this.writeSet(false);
	}
	
	public void writeSet(boolean force) throws IOException{
		if(!force && this.stringSet.size() < flushSize)
			return;
		
		for (String curWord : this.stringSet) {

			this.outfile.write(curWord);
			this.outfile.write(System.getProperty("line.separator")); 
		}
		this.wordCount+= this.stringSet.size();
		this.stringSet.clear();
		
		System.out.println(this.writeCount+++", "+this.wordCount);		
	}

}
